package com.sline.stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo6_TryFinally {
    public static void main(String[] args) throws Exception {
        // demo1();

        try(
            FileInputStream fis = new FileInputStream("abc.png");
            FileOutputStream fos = new FileOutputStream("copy.png");
            MyClose mc = new MyClose();
        ){
            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
        }
    }

    private static void demo1() throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("abc.png");
            fos = new FileOutputStream("copy.png");

            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }

        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }
        }
    }
}

class MyClose implements AutoCloseable {
    @Override
    public void close() throws Exception {
        System.out.println("我关了");
    }
}